<template>
    <div class="main-container">
        <div class="main">
            <div class="main-box">
                <!-- 轮播图 -->
                <el-carousel class="banner" :interval="5000" arrow="always">
                    <el-carousel-item v-for="(item, index) in bannerList" :key="index" @click="bannerClike(item)">
                        <a href="javascript:;" @click="handleClick(item.id)">
                            <el-image class="bannerImg" :src="item.avatar"></el-image>
                            <h3 class="title">{{ item.title }}</h3>
                        </a>
                    </el-carousel-item>
                </el-carousel>
                <!-- 热门分类 -->
                <div class="hot_category">
                    <div class="title">
                        <span>精品分类</span>
                        <a href="/categorys"><i class="iconfont icon-fenlei"></i>全部分类</a>
                    </div>
                    <div class="categoryList">
                        <div class="item" v-for="item in categoryList" :key="item.id">
                            <a href="javascript:;" @click="handleCategoryClike(item)">
                                <img @click="" :src="item.avatar" :alt="item.name" />
                                <span class="name">{{ item.name }}</span>
                            </a>
                        </div>
                    </div>
                </div>
                <!-- 文章列表 -->
                <div class="artcile_main">
                    <div class="title">
                        <span>文章列表</span>
                    </div>
                    <div class="mainBox">
                        <ul class="ul_item" v-for="item in articleList">
                            <li class="main_li" @click="handleClick(item.id)" data-wow-duration="2s" data-wow-iteration="1">
                                <div href="" class="thumbnail">
                                    <img class="article_cover" :src="item.avatar" :alt="item.title"
                                        @error="item.avatar = img" />
                                    <i class="iconfont icon-tupian"></i>
                                    <span class="time">{{ item.createTime }}</span>
                                </div>
                                <div class="information">
                                    <div class="titleName">
                                        <span v-if="item.isStick == 1" class="top">置顶</span>
                                        <span v-if="item.isOriginal == 1" class="original">原创</span>
                                        {{ item.title }}
                                    </div>
                                    <div class="info">
                                        {{ item.summary }}
                                    </div>
                                    <div class="meta">
                                        <ul class="items">
                                            <li>{{ item.username }}</li>
                                            <li>{{ item.createTime }}</li>
                                            <li><i class="iconfont icon-yuedu"></i>{{ item.quantity }}</li>
                                            <li><i class="el-icon-chat-dot-round"></i>{{ item.commentCount }}</li>
                                            <li><i class="iconfont icon-dianzan1"></i>{{ item.likeCount }}</li>
                                        </ul>
                                        <ul class="items">
                                            <li v-for="tag in item.tagList">
                                                <i class="iconfont icon-biaoqian"></i>
                                                <span class="tag_name" @click="">{{ tag.name }}</span>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </li>

                        </ul>
                    </div>
                </div>
            </div>
            <!-- 分页按钮 -->
            <div class="page" v-show="pageData.pageNo < pageTotal" @click="onPage">
                加载更多
            </div>
        </div>

        <Sidebar />
    </div>
</template>

<script>
import { fetchArticleList, featchHomeData } from '@/api'

import Sidebar from '@/components/sidebar/index.vue'
export default {
    components: {
        Sidebar,
    },
    name: 'Home',
    data() {
        return {
            emojis: [],
            pageData: {
                pageNo: 1,
                pageSize: 6,
            },
            bannerList: [],
            categoryList: [],
            articleList: [],
            pageTotal: 0,
        };
    },
    // require引用：
    mounted() {
        this.fetchArticleList()
        this.getHomeData()
    },
    methods: {
        bannerClike(item) {
            console.log(item)
        },
        // 分页
        onPage() {
            this.pageData.pageNo++;
            this.fetchArticleList()
        },
        fetchArticleList() {
            fetchArticleList(this.pageData).then(res => {
                this.articleList.push(...res.data.records);
                this.pageTotal = res.data.pages
            })
        },
        getHomeData() {
            featchHomeData().then(res => {
                this.categoryList = res.extra.categories
                this.bannerList = res.extra.articles
            })
        },
        handleClick(id) {
            this.$router.push({ path: '/articleInfo', query: { articleId: id } })
        },
        handleCategoryClike(item) {
            this.$router.push({ name: "/category", query: { id: item.id, name: item.name } })
        }
    },
}
</script>

<style lang="scss" scoped >
.page {
    text-align: center;
    background-color: rgba(0, 0, 0, .8);
    width: 120px;
    height: 30px;
    line-height: 30px;
    border-radius: 50px;
    margin: 0 auto;
    margin-top: 20px;
    cursor: pointer;
    color: #fff;
}

.banner {
    position: relative;
    border-top-right-radius: 10px;
    border-top-left-radius: 10px;

    height: 420px;

    /deep/ .el-carousel__container {
        height: 100%;
    }

    .title {
        position: absolute;
        height: 50px;
        line-height: 50px;
        text-align: center;
        width: 100%;
        bottom: 50px;
        color: #fff;
        background: rgba(0, 0, 0, .3);
        z-index: 1;
    }

    .bannerImg {
        width: 100%;
        height: 100%;
    }
}

@media screen and (max-width: 1118px) {
    .main-container {
        display: flex;
        justify-content: center;
        width: 100%;

        .main {
            width: 100%;
            margin-top: 60px;

            .main-box {
                .banner {
                    border-radius: 0;
                    height: 250px
                }

                .hot_category {
                    width: 100%;
                    height: auto;
                    background-color: var(--background-color);

                    .title {
                        height: 45px;
                        color: var(--theme-color);
                        position: relative;
                        border-bottom: 1px solid var(--border-line);
                        margin-left: 15px;

                        span {
                            display: inline-block;
                            font-size: 14px;
                            line-height: 45px;
                            font-weight: 700;
                        }

                        a {
                            display: inline-block;
                            text-decoration: none;
                            color: grey;
                            font-size: 14px;
                            line-height: 45px;
                            position: absolute;
                            right: 0;
                            margin-right: 10px;

                            &:hover {
                                color: var(--theme-color);
                            }

                            i {
                                line-height: 45px;
                                padding-right: 2px;
                            }

                        }

                    }

                    .categoryList {
                        margin-top: 10px;
                        width: 100%;
                        display: flex;

                        div {
                            height: 122px;
                            display: inline-block;
                            position: relative;
                            margin: 0 5px;
                            width: 100%;
                        }

                        .item {
                            overflow: hidden;
                            border-radius: 10px;
                            width: 100%;

                            img {
                                width: 100%;
                                height: 100%;
                                border-radius: 10px;
                                transition: all 0.5s;
                            }

                            .name {
                                width: 100%;
                                position: absolute;
                                color: var(--baise);
                                background: rgba(0, 0, 0, .4);
                                font-size: 14px;
                                display: block;
                                text-align: center;
                                top: 0;
                                bottom: 0;
                                left: 0;
                                right: 0;
                                line-height: 122px;
                                border-radius: 10px;
                            }

                            &:hover {
                                img {
                                    transform: scale(1.2);
                                }

                                span {
                                    color: #49b1f5;
                                }
                            }
                        }

                    }
                }

                .artcile_main {
                    background-color: var(--background-color);
                    width: 100%;

                    .title {
                        height: 45px;
                        padding-left: 10px;
                        color: var(--theme-color);
                        position: relative;
                        border-bottom: 1px solid var(--border-line);

                        span {
                            display: inline-block;
                            font-size: 14px;
                            line-height: 45px;
                            font-weight: 700;
                        }
                    }

                    .mainBox {
                        width: 100%;

                        .ul_item {
                            padding-left: 5px;
                            cursor: pointer;
                            border-bottom: 1px solid var(--border-line);

                            &:last-child {
                                border: 0;
                            }

                            .main_li {
                                list-style: none;
                                display: flex;
                                position: relative;
                                padding-bottom: 18px;
                                margin-top: 18px;
                                width: 100%;


                                .thumbnail {
                                    flex-shrink: 0;
                                    position: relative;
                                    width: 150px;
                                    height: 120px;
                                    margin-right: 15px;
                                    overflow: hidden;
                                    text-decoration: none;

                                    .article_cover {
                                        border-radius: 4px;
                                        object-fit: cover;
                                        width: 100%;
                                        height: 100%;
                                    }

                                    .iconfont {
                                        position: absolute;
                                        top: 2px;
                                        left: 5px;
                                        color: var(--background-color);
                                        font-size: 18px;
                                    }

                                    .time {
                                        display: none;
                                    }
                                }

                                .information {
                                    display: flex;
                                    -webkit-box-orient: vertical;
                                    -webkit-box-direction: normal;
                                    -ms-flex-direction: column;
                                    flex-direction: column;
                                    -webkit-box-flex: 1;
                                    -ms-flex: 1;
                                    flex: 1;
                                    min-width: 0;
                                    width: 100%;

                                    .titleName {
                                        margin-bottom: 10px;
                                        color: var(--article-color);
                                        font-size: 18px;
                                        line-height: 24px;
                                        max-height: 48px;
                                        -webkit-transition: color .35s;
                                        transition: color .35s;
                                        text-decoration: none;
                                        overflow: hidden;
                                        text-overflow: ellipsis;
                                        white-space: nowrap;
                                        width: 100%;

                                        .top,
                                        .original {
                                            display: none;
                                        }
                                    }

                                    .info {
                                        color: var(--text-color);
                                        line-height: 22px;
                                        max-height: 44px;
                                        opacity: .85;
                                        text-decoration: none;
                                        width: 100%;
                                        overflow: hidden;
                                        text-overflow: ellipsis;
                                        white-space: nowrap;
                                    }

                                    .meta {
                                        -webkit-box-pack: justify;
                                        -ms-flex-pack: justify;
                                        justify-content: space-between;
                                        margin-top: auto;
                                        color: var(--text-color);
                                        font-size: 13px;
                                        -webkit-box-align: center;
                                        -ms-flex-align: center;
                                        align-items: center;
                                        width: 100%;

                                        .items {
                                            -webkit-box-pack: start;
                                            -ms-flex-pack: start;
                                            justify-content: flex-start;
                                            -ms-flex-negative: 0;
                                            flex-shrink: 0;
                                            -webkit-box-align: center;
                                            -ms-flex-align: center;
                                            align-items: center;
                                            padding: 0 0;
                                            width: 100%;

                                            li {
                                                list-style: none;
                                                display: inline-block;

                                                &:after {
                                                    content: "/";
                                                    color: #c0c4cc;
                                                    padding: 0 5px;
                                                }

                                                &:last-child:after {
                                                    content: "";

                                                }

                                                i {
                                                    padding: 0 3px;
                                                    font-size: 13px;
                                                }

                                                .tag_name {
                                                    color: var(--text-color);
                                                    text-decoration: none;
                                                    width: 100%;

                                                    &:hover {
                                                        cursor: pointer;
                                                        color: var(--theme-color);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }


                }
            }
        }

    }
}

@media screen and (min-width: 1119px) {
    .main-container {
        display: flex;
        justify-content: center;

        .main {
            width: 55%;
            margin-top: 80px;

            .main-box {
                width: 100%;
                border-radius: 10px;
                background-color: var(--background-color);

                &:hover {
                    box-shadow: 5px 4px 8px 6px rgba(7, 17, 27, .06);
                    transition: all .3s;
                }

                .hot_category {
                    height: 179px;

                    .title {
                        height: 45px;
                        color: var(--theme-color);
                        position: relative;
                        border-bottom: 1px solid var(--border-line);
                        margin-left: 15px;

                        span {
                            display: inline-block;
                            font-size: 14px;
                            line-height: 45px;
                            font-weight: 700;
                        }

                        a {
                            display: inline-block;
                            text-decoration: none;
                            color: grey;
                            font-size: 14px;
                            line-height: 45px;
                            position: absolute;
                            right: 0;
                            margin-right: 10px;

                            &:hover {
                                color: var(--theme-color);
                            }

                            i {
                                line-height: 45px;
                                padding-right: 2px;
                            }

                        }

                    }

                    .categoryList {
                        margin-left: 5px;
                        margin-top: 10px;
                        display: flex;
                        justify-content: space-around;

                        div {
                            height: 122px;
                            display: inline-block;
                            position: relative;
                            margin: 0 5px;

                        }

                        .item {
                            overflow: hidden;
                            border-radius: 10px;
                            width: 190px;
                            margin-left: 10px;

                            img {
                                height: 100%;
                                width: 100%;
                                border-radius: 10px;
                                transition: all 0.5s;
                            }

                            .name {
                                position: absolute;
                                color: var(--baise);
                                background: rgba(0, 0, 0, .4);
                                font-size: 14px;
                                display: block;
                                text-align: center;
                                top: 0;
                                bottom: 0;
                                left: 0;
                                right: 0;
                                line-height: 122px;
                                border-radius: 10px;
                            }

                            &:hover {
                                img {
                                    transform: scale(1.2);
                                }

                                span {
                                    color: #49b1f5;
                                }
                            }
                        }

                    }
                }

                .artcile_main {

                    .title {
                        height: 45px;
                        color: var(--theme-color);
                        position: relative;
                        border-bottom: 1px solid var(--border-line);
                        margin-left: 15px;

                        span {
                            display: inline-block;
                            font-size: 14px;
                            line-height: 45px;
                            font-weight: 700;
                        }
                    }

                    .ul_item {
                        padding-left: 20px;
                        cursor: pointer;
                        border-bottom: 1px solid var(--border-line);

                        &:last-child {
                            border: 0;
                        }

                        &:hover {
                            .article_cover {
                                transform: scale(1.2);
                            }

                            .time {
                                transform: translateX(-110%) translateY(5%);
                                transition: all 0.5s;
                            }

                            .main_li .information .titleName,
                            .main_li .information .info {
                                color: var(--theme-color);
                            }

                            .main_li::before {
                                transform: scaleX(1);
                            }

                        }

                        .main_li {
                            list-style: none;

                            display: flex;
                            position: relative;
                            padding-bottom: 18px;
                            margin-top: 18px;



                            &::before {
                                content: "";
                                width: 4px;
                                height: 30px;
                                background-color: var(--theme-color);
                                position: absolute;
                                left: -15px;
                                border-radius: 50px;
                                transform: scaleX(0);
                                transition: all 0.5s;
                            }

                            .thumbnail {
                                flex-shrink: 0;
                                position: relative;
                                width: 210px;
                                height: 140px;
                                margin-right: 15px;
                                overflow: hidden;
                                text-decoration: none;

                                .article_cover {
                                    border-radius: 4px;
                                    object-fit: cover;
                                    width: 100%;
                                    height: 100%;
                                    transition: all 0.3s;
                                }

                                .iconfont {
                                    position: absolute;
                                    top: 2px;
                                    left: 5px;
                                    color: var(--background-color);
                                    font-size: 18px;
                                }

                                .time {
                                    display: block;
                                    padding: 5px 5px;
                                    border-radius: 20px;
                                    position: absolute;
                                    font-size: 12px;
                                    top: 3px;
                                    right: -38%;
                                    background-color: var(--theme-color);
                                    color: var(--background-color);
                                }
                            }

                            .information {
                                display: flex;
                                -webkit-box-orient: vertical;
                                -webkit-box-direction: normal;
                                -ms-flex-direction: column;
                                flex-direction: column;
                                -webkit-box-flex: 1;
                                -ms-flex: 1;
                                flex: 1;
                                min-width: 0;

                                .titleName {
                                    margin-bottom: 10px;
                                    color: var(--article-color);
                                    font-size: 18px;
                                    line-height: 24px;
                                    max-height: 48px;
                                    -webkit-transition: color .35s;
                                    transition: color .35s;
                                    text-decoration: none;
                                    overflow: hidden;
                                    text-overflow: ellipsis;
                                    white-space: nowrap;
                                    width: 100%;

                                    .top,
                                    .original {
                                        height: 20px;
                                        padding: 0 6px;
                                        margin-right: 5px;
                                        line-height: 20px;
                                        font-size: 12px;
                                        white-space: nowrap;
                                        vertical-align: 2px;
                                        color: var(--background-color);

                                        border-radius: 2px 6px;
                                        display: inline-block;
                                    }

                                    .top {
                                        background-image: -webkit-linear-gradient(0deg, #3ca5f6 0, #a86af9 100%);
                                    }

                                    .original {
                                        background-image: -webkit-linear-gradient(0deg, #26A69A 0, #00897B 100%);
                                    }
                                }

                                .info {
                                    color: var(--text-color);
                                    line-height: 22px;
                                    max-height: 44px;
                                    opacity: .85;
                                    text-decoration: none;
                                    width: 100%;
                                    overflow: hidden;
                                    text-overflow: ellipsis;
                                    white-space: nowrap;
                                }

                                .meta {
                                    -webkit-box-pack: justify;
                                    -ms-flex-pack: justify;
                                    justify-content: space-between;
                                    margin-top: auto;
                                    color: var(--text-color);
                                    font-size: 13px;
                                    display: flex;
                                    -webkit-box-align: center;
                                    -ms-flex-align: center;
                                    align-items: center;

                                    .items {
                                        -webkit-box-pack: start;
                                        -ms-flex-pack: start;
                                        justify-content: flex-start;
                                        -ms-flex-negative: 0;
                                        flex-shrink: 0;
                                        display: flex;
                                        -webkit-box-align: center;
                                        -ms-flex-align: center;
                                        align-items: center;
                                        padding: 0 0;

                                        li {
                                            list-style: none;
                                            display: inline-block;

                                            &:after {
                                                content: "/";
                                                color: #c0c4cc;
                                                padding: 0 5px;
                                            }

                                            &:last-child:after {
                                                content: "";

                                            }

                                            i {
                                                padding: 0 3px;
                                                font-size: 13px;
                                            }

                                            .tag_name {
                                                color: var(--text-color);
                                                text-decoration: none;

                                                &:hover {
                                                    cursor: pointer;
                                                    color: var(--theme-color);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                }

            }


        }

    }
}
</style>